/*
 * @(#)DataModelResultTestCase.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.result;

import java.util.HashMap;
import java.util.Map;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import com.opensymphony.xwork.ActionContext;

import com.nhncorp.lucy.common.config.model.ApplicationInfo;
import com.nhncorp.lucy.web.helper.ServletHelper;
import com.nhncorp.usf.core.UsfDispatcher;
import com.nhncorp.usf.core.config.runtime.ResultPageInfo;
import com.nhncorp.usf.core.config.runtime.ResultPageInfo.DISPATCHER_TYPE;
import com.nhncorp.usf.core.result.direct.HtmlResult;
import junit.framework.Assert;

/**
 * {@link HtmlResult} TestCase.
 *
 * @author Web Platform Development Team
 */
public class DataModelResultTestCase {
    AbstractResult result = new HTMLDataModelResult();

    @BeforeClass
    public static void initialize() throws Exception {
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        ActionContext.getContext().put(ServletHelper.HTTP_REQUEST, request);
        ActionContext.getContext().put(ServletHelper.HTTP_RESPONSE, response);

        ApplicationInfo.setPageEncoding("utf-8");
    }

    @Test
    public void executeResult() throws Exception {
        ResultPageInfo resultPageInfo = new ResultPageInfo(DISPATCHER_TYPE.direct, "");

        MockHttpServletResponse response = (MockHttpServletResponse)ServletHelper.getResponse();
        try {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("result", "abc");
            Map<String, Object> requestParam = new HashMap<String, Object>();
            requestParam.put("input", "bcd");
            dataMap.put(UsfDispatcher.REQUEST_PARAM_NAME, requestParam);
            result.execute(resultPageInfo, dataMap);
            String contents = response.getContentAsString().trim();
            System.out.println(contents);
            Assert.assertTrue(contents.indexOf("result") > 0);
            Assert.assertTrue(contents.indexOf("abc") > 0);
            Assert.assertTrue(contents.indexOf("String") > 0);
            Assert.assertTrue(contents.indexOf("input") > 0);
            Assert.assertTrue(contents.indexOf("bcd") > 0);
            Assert.assertTrue(contents.indexOf("String") > 0);
        } catch(Exception except) {
            Assert.fail();
        }
    }
}
